<?php

namespace app\ljhs\controller;
use app\common\controller\Ljhs;
use app\admin\model\AdminLog;
use fast\Random;
use think\Config;
use think\Hook;
use think\Validate;
use app\common\model\RkComfj;
use app\common\model\RkQtfj;
use app\common\model\RkJg;
/**
 * 库存接口
 */
class Depot extends Ljhs
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 获取企业列表
     * @return void
     */
    public function getCom()
    {
        $stand = $this->auth->stand_id;
        $keywors = $this->request->post('keywors');
        $com = \app\common\model\Com::where('stand_id',$stand)
            ->where('name','like','%'.$keywors.'%')
            ->field('id,name,com_no')->select();
        $this->success(__('获取成功'),$com);
    }

    /**
     * 企业到分拣入库
     * @return void
     */
    public function rkComfj()
    {
        $comdata = $this->request->post('comdata/a');
        if(!$comdata){
            $this->error(__('请选择企业'));
        }
        $cat_time = $this->request->post('car_time');
        if(!$cat_time){
            $this->error(__('请选择车次时间'));
        }
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $dataAll = [];
        $total_ratio = 0;
        foreach ($comdata as $key => $value){
            $insert = [
                'user_id'=> $user['id'],
                'com_id' => $value['com_id'],
                'ratio'=> $value['ratio'],
                'car_no' => $car_no,
                'car_time' => strtotime($cat_time),
                'stand_id' => $user['stand_id'],
                'createtime' => time()
            ];
            $dataAll[] = $insert;
            $total_ratio += $value['ratio'];
        }
        if($total_ratio != 100){
            $this->error(__('比例总和必须为100'));
        }
        $res = RkComfj::insertAll($dataAll);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 其他到分拣入库
     * @return void
     */
    public function rkQtFj()
    {
        $com_name = $this->request->post('com_name');
        if(!$com_name){
            $this->error(__('请输入企业信息'));
        }
        $cat_time = $this->request->post('car_time');
        if(!$cat_time){
            $this->error(__('请选择车次时间'));
        }
        $km = $this->request->post('km');
        $price = $this->request->post('price');
        $pay_type = $this->request->post('pay_type');
        if(!$km || !$price || !$pay_type){
            $this->error(__('参数错误'));
        }
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $dataAll = [
            'user_id'=> $user['id'],
            'com_name' => $com_name,
            'km' => $km,
            'price' => $price,
            'pay_type' => $pay_type,
            'car_no' => $car_no,
            'car_time' => strtotime($cat_time),
            'ratio'=>100,
            'stand_id' => $user['stand_id'],
            'createtime' => time()
        ];

        $res = RkQtfj::create($dataAll);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 秸秆入库
     * @return void
     */
    public function rkJg()
    {
        $com_name = $this->request->post('com_name');
        if(!$com_name){
            $this->error(__('请输入企业信息'));
        }
        $cat_time = $this->request->post('car_time');
        if(!$cat_time){
            $this->error(__('请选择车次时间'));
        }
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $dataAll = [
            'user_id'=> $user['id'],
            'com_name' => $com_name,
            'car_no' => $car_no,
            'ratio'=>100,
            'car_time' => strtotime($cat_time),
            'stand_id' => $user['stand_id'],
            'createtime' => time()
        ];
        $res = RkJg::create($dataAll);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 企业到电厂出库
     * @return void
     */
    public function ckComDc()
    {
        $com_name = $this->request->post('com_name');
        if(!$com_name){
            $this->error(__('请输入企业信息'));
        }
        $cat_time = $this->request->post('car_time');
        if(!$cat_time){
            $this->error(__('请选择车次时间'));
        }
        $ck_images = $this->request->post('ck_images');
        $kg = $this->request->post('kg');
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $dataAll = [
            'user_id'=> $user['id'],
            'com_name' => $com_name,
            'ck_images'=> $ck_images,
            'kg' => $kg,
            'car_no' => $car_no,
            'car_time' => strtotime($cat_time),
            'stand_id' => $user['stand_id'],
            'createtime' => time()
        ];
        $res = \app\common\model\CkComdc::create($dataAll);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 分拣中心到电厂出库
     * @return void
     */
    public function ckFjDc()
    {
        $com_name = $this->request->post('com_name');
        if(!$com_name){
            $this->error(__('请输入企业信息'));
        }
        $cat_time = $this->request->post('car_time');
        if(!$cat_time){
            $this->error(__('请选择车次时间'));
        }
        $ck_images = $this->request->post('ck_images');
        $kg = $this->request->post('kg');
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $dataAll = [
            'user_id'=> $user['id'],
            'com_name' => $com_name,
            'ck_images'=> $ck_images,
            'kg' => $kg,
            'car_no' => $car_no,
            'car_time' => strtotime($cat_time),
            'stand_id' => $user['stand_id'],
            'createtime' => time()
        ];
        $res = \app\common\model\CkFjdc::create($dataAll);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 其他到电厂出库
     * @return void
     */
    public function ckQtdc()
    {
        $comdata = $this->request->post('comdata/a');
        if(!$comdata){
            $this->error(__('请选择企业'));
        }
        $cat_time = $this->request->post('car_time');
        if(!$cat_time){
            $this->error(__('请选择车次时间'));
        }
        $price = $this->request->post('price');
        if(!$price){
            $this->error(__('请输入金额'));
        }
        $pay_type = $this->request->post('pay_type');
        if(!$pay_type){
            $this->error(__('请选择支付渠道'));
        }
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $kg = $this->request->post('kg');
        $dataAll = [];
        $total_ratio = 0;
        foreach ($comdata as $key => $value){
            $insert = [
                'user_id'=> $user['id'],
                'com_id' => $value['com_id'],
                'ratio'=> $value['ratio'],
                'price' => $price,
                'kg' => $kg,
                'pay_type' => $pay_type,
                'car_no' => $car_no,
                'car_time' => strtotime($cat_time),
                'stand_id' => $user['stand_id'],
                'createtime' => time()
            ];
            $dataAll[] = $insert;
            $total_ratio += $value['ratio'];
        }
        if($total_ratio != 100){
            $this->error(__('比例总和必须为100'));
        }
        $res = \app\common\model\CkQtdc::insertAll($dataAll);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 秸秆出库
     * @return void
     */
    public function ckJg()
    {
        $com_name = $this->request->post('com_name');
        if(!$com_name){
            $this->error(__('请输入企业信息'));
        }
        $cat_time = $this->request->post('car_time');
        if(!$cat_time){
            $this->error(__('请选择车次时间'));
        }
        $ck_images = $this->request->post('ck_images');
        $kg = $this->request->post('kg');
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $dataAll = [
            'user_id'=> $user['id'],
            'com_name' => $com_name,
            'ck_images'=> $ck_images,
            'kg' => $kg,
            'car_no' => $car_no,
            'car_time' => strtotime($cat_time),
            'stand_id' => $user['stand_id'],
            'createtime' => time()
        ];
        $res = \app\common\model\CkJg::create($dataAll);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }


}
